﻿using UnityEngine;
using System.Collections;
using System.Diagnostics;
using System;
//public static class ObjectEx{

//}

public enum DebugType{
	Defualt,
	Finger,
	SkillMgr,

	DebufCount,
}
public class Debuger:MonoBehaviour{
	static Debuger instance;
	public static Debuger Instance{
		get{
			if(instance == null)
				instance = GameObject.FindObjectOfType<Debuger>();
			if(instance == null){
				instance = new GameObject("DebugSeting").AddComponent<Debuger>();
				instance.Rest();
				GameObject.DontDestroyOnLoad(instance);
			}
			return instance;
		}
	}
	public void Rest(){
		data = new DebugData[(int)DebugType.DebufCount];
		for(int i = 0;i < (int)DebugType.DebufCount;i++){
			data[i] = new DebugData(((DebugType)(i)).ToString());
		}
		data [(int)(DebugType.Defualt)].debugEnable = true;
	}
	public DebugType _debugType;
	public bool enable = true;
	public bool logGameTime = true;
	public bool logStack = true;
	public bool logField = true;
	public DebugData[] data;
}
[System.Serializable]
public class DebugData{
	public DebugData(string _name){
		debugName = _name;
	}
	public string debugName;
	public bool debugEnable;
}
static public class DebugObjEx{
	public static void DebugLog<T>(this T example,string log,DebugType debugtype = DebugType.Defualt) where T : UnityEngine.Object
	{
		if (example == null)
			return;
		if (!Debuger.Instance.enable)
			return;
		if (!Debuger.Instance.data [(int)debugtype].debugEnable)
			return;
		string _log = log+"\n";
		StackFrame sf = new StackFrame (1);
		StackTrace st = new StackTrace(sf);
		if (Debuger.Instance.logGameTime)
			_log += "Time:" + Time.time + "\n";
		if (Debuger.Instance.logStack) {
			_log += "调用者名称:"+example.name +" 函数名称 "+st.ToString()+"\n";
		}
		if (Debuger.Instance.logField) {
			var _type = sf.GetMethod().ReflectedType;
			System.Reflection.FieldInfo[] fields = example.GetType().GetFields();
			_log+= "--------------参数开始------------\n";
			for(int i =0;i < fields.Length;i++){
				var v1 = _type.GetField(fields[i].Name).GetValue(example);
				_log+="变量名称:"+fields[i].Name;
				if(v1!=null){
					_log +="  值:"+ v1.ToString().PadRight(15,' ')+"\n";
				}
				else{
					_log+= "  值: Null -------\n";
				}
			}
			_log+= "--------------参数结束------------\n";
		}

		UnityEngine.Debug.Log (_log);
	}
}